package org.su.webadapter.bind;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class SqlDateEditor extends AbstractParameterEditor {
	private final DateFormat dateFormat;

	public SqlDateEditor(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}

	public SqlDateEditor() {
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
	}

	public Object parseText(String text) {
		Object result = null;
		if (StringUtils.isNotBlank(text)) {
			
				try {
					result = new  java.sql.Date(dateFormat.parse(text).getTime());
				} catch (ParseException e) {
					throw new ParamBindException(e);
				}
			
		}
		
		return result;
	}
}
